﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SadlandVillage.Core;
using SadlandVillage.Core.Helper;

namespace SadlandVillage
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Map_old : GameScene
    {
        #region Members and Properties
        Sprite s_mainChar;
        Sprite s_mainCharHouse;
        Sprite s_gamerHouse;
        Sprite s_forestHouse;
        Sprite s_riverHouse;
        Sprite s_restaurantHouse;
        Sprite s_mansionHouse;
        Sprite s_danceHouse;
        Sprite s_friendHouse;

        Sprite s_rufus;
        Sprite s_bell;
        Sprite s_goodwin;
        Sprite s_line;
        Sprite s_clotilde;


        Rectangle[] recSprite = new Rectangle[16]; //Posicoes do boneco -> 12 + 4 rotacionado
        int m_Move = 0;
        int lastX = 0;
        int lastY = 0;

        // An array of "Texture2D" objects to hold our Tile Set
        Texture2D t2dTileSet;
        // Tileset Size Information
        int iTileSetXCount = 12;

        const int iMapWidth = 30;
        const int iMapHeight = 24;
        // Our simple integer-array based map
        // Our map's base terrain layer.  These should be tiles 0 thru 11, which are full
        // square tiles.  The base map layer is drawn without alpha blending.
        #region Map-terrain definitions
        int[,] iMap = new int[iMapHeight, iMapWidth] { 
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,108,108,108,108},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,004,004,108,108},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,108,108,108,108},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,108,108,108,108,108},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,003,000,000,000,000,108,108,108,108,108,108},
          {000,003,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,003,003,000,000,000,108,108,108,108,108,108,108},
          {000,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,000,000,000,108,108,108,108,108,108,108,108},
          {000,000,000,000,000,003,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,108,108,108,108,108,108,108,108,108},
          {108,108,108,108,108,004,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108},
          {108,108,108,108,108,004,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108},
          {108,108,108,108,108,004,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,000},
          {108,108,108,108,108,004,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,000,000,000},
          {000,000,000,000,000,003,000,000,000,000,000,000,000,000,000,000,000,000,108,108,108,108,108,108,108,108,000,000,000,000},
          {000,000,000,003,003,003,003,003,003,003,003,003,003,003,003,003,003,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,000,003,000,000,000,000,003,000,000,000,000,000,000,003,003,003,003,003,004,004,004,004,003,003,003,000,000,000},
          {000,000,000,003,000,000,000,000,003,000,000,000,000,000,000,003,000,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,003,003,000,000,000,000,000,000,000,000,000,000,000,003,000,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,000,003,000,000,000,000,000,000,000,000,000,000,000,003,000,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,000,003,003,000,000,000,000,000,000,000,000,000,000,003,000,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,000,003,000,000,000,000,000,000,000,000,000,000,000,003,000,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,000,003,000,000,000,000,000,000,003,000,000,000,000,003,000,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,108,108,108,108,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,108,108,108,108,000,000,000,000,000,000}
        };
        // Our map's Transition layer.  This layer is drawn using Alpha Blending, and any
        // tile with a value of 11 or less WILL NOT BE DRAWN.  They can be filled in here
        // for visulization's sake.
        int[,] iMapTrans = new int[iMapHeight, iMapWidth] { 
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,015,002,002,002},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,004,002,002,002},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,015,002,002,002},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,018,023,002,002,002},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,044,041,003,039,000,000,000,018,023,002,002,002,002},
          {038,003,040,036,036,036,036,036,036,036,036,036,036,036,036,036,036,041,003,003,039,000,000,018,023,002,002,002,002,002},
          {038,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,042,047,000,018,023,002,002,002,000,002,002},
          {046,037,037,037,043,003,042,037,037,037,037,037,037,037,037,037,037,037,037,047,000,018,023,002,002,002,002,002,002,002},
          {013,013,013,013,013,004,013,013,013,013,013,013,013,013,013,013,013,013,013,013,013,023,002,002,002,002,002,002,002,002},
          {002,002,002,002,062,004,063,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,020,012},
          {002,002,002,002,062,004,063,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,002,020,012,017,000},
          {012,012,012,012,012,004,012,012,012,012,012,012,012,012,012,012,012,012,021,002,002,002,002,002,002,020,017,000,000,000},
          {000,000,044,036,041,003,040,036,036,036,036,036,036,036,036,036,036,045,016,012,021,002,002,020,012,017,000,000,000,000},
          {000,000,038,003,003,003,003,003,003,003,003,003,003,003,003,003,003,040,036,036,015,060,060,014,036,036,036,000,000,000},
          {000,000,038,003,042,037,037,043,003,042,037,037,037,037,043,003,003,003,003,003,004,004,004,004,003,003,003,000,000,000},
          {000,000,041,003,039,000,000,038,003,039,000,000,000,000,038,003,042,037,037,037,015,061,061,014,037,037,037,000,000,000},
          {000,000,003,003,039,000,000,000,000,000,000,000,000,000,038,003,039,000,000,000,015,002,002,014,000,000,000,000,000,000},
          {000,000,043,003,040,000,000,000,000,000,000,000,000,000,038,003,039,000,000,000,015,002,002,014,000,000,000,000,000,000},
          {000,000,038,003,003,000,000,000,000,000,000,000,000,000,038,003,039,000,000,000,015,002,002,014,000,000,000,000,000,000},
          {000,000,038,003,042,000,000,000,000,000,000,000,000,000,038,003,039,000,000,000,015,002,002,014,000,000,000,000,000,000},
          {000,000,041,003,040,036,036,036,036,041,003,040,036,036,041,003,040,036,000,000,015,002,002,014,000,000,000,000,000,000},
          {000,000,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,003,000,000,015,002,002,014,000,000,000,000,000,000},
          {000,000,037,037,037,037,037,037,037,037,037,037,037,037,037,037,037,037,000,000,015,002,002,014,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,015,002,002,014,000,000,000,000,000,000}
        };
        // Our map's Object layer.  This layer is drawn using Alpha Blending, and any
        // tile with a value of 11 or less WILL NOT BE DRAWN.  They can be filled in here
        // for visulization's sake.
        int[,] iMapObjects = new int[iMapHeight, iMapWidth] { 
          {000,000,000,000,098,000,000,098,098,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,060,060,000,000},
          {000,000,000,000,098,000,097,098,000,098,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,004,004,000,000},
          {000,000,000,000,000,097,000,098,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,061,061,000,000},
          {000,000,000,000,097,000,098,000,098,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,098,098,000,096,000,098,097,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,101,000,000,000,104,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,102,000,000,000,000,000,000,000,000,000},
          {000,101,000,000,062,000,063,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,104,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,062,000,063,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,103,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,060,000,000,060,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,103,000,000,000,000,000,000,000,061,000,000,061,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,105,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,105,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,105,000,000,000,000,105}
        };

        int[,] iMapWalkable = new int[iMapHeight, iMapWidth] { 
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000},
          {000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,001,001,000,000,000,000,000,000,000,000,000,000},
          {000,001,001,001,001,001,001,001,001,001,001,001,001,001,001,001,001,001,001,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,001,001,001,001,001,001,001,001,001,001,001,001,001,001,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,001,000,000,000,000,001,000,000,000,000,000,000,001,001,001,001,001,001,001,001,001,001,001,001,000,000,000},
          {000,000,000,001,000,000,000,000,001,000,000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,001,001,000,000,000,000,000,000,000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,001,001,000,000,000,000,000,000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,001,000,000,000,000,000,000,001,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,001,001,001,001,001,001,001,001,001,001,001,001,001,001,001,001,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000},
          {000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000}
        };
        #endregion
        // Variable we will need for Keyboard Input
        KeyboardState ksKeyboardState;
        //Map coordinates for upper left corner
        int iMapX = 0;
        int iMapY = 0;
        // How far from the Upper Left corner of the display do we want our map to start
        public static int iMapDisplayOffsetX = 16;
        public static int iMapDisplayOffsetY = 12;
        // How many tiles should we display at a time
        public static int iMapDisplayWidth = 30;
        public static int iMapDisplayHeight = 24;
        // The size of an individual tile in pixels
        public static int iTileWidth = 24;
        public static int iTileHeight = 24;
        // How rapidly do we want the map to scroll?
        float fTotalElapsedTime = 0;
        //this is the object that will draw the sprites

        // Sub-tile coordinates for Smooth Scrolling
        int iMapXOffset = 0;
        int iMapYOffset = 0;
        // Determines how fast the map will scroll (pixels per arrow key press)
        // Variable to determine if we are still moving from the previous directional command

        // What direction are we moving (0=up, 1=down, 2=left, 3=right)
        int iMoveDirection = 0;


        // Water Animation
        int iTileAnimationFrame = 0;         // The current frame of animation we are on.
        int iTileAnimationFrameCount = 7;    // The total number of frames in our animation
        int iTileAnimationStartFrame = 108;  // The beginning frame of our animation
        float fAnimationTime = 0.0f;         // How much time has elapsed since we last animated
        float fAnimationDelay = 0.1f;        // How much time needs to elapse before we animate

        byte canWalk = 3;

        #endregion

        public Map_old(ContentManager content, SpriteBatch spriteBatch)
                :base(content, spriteBatch)
        {
            //this.graphics.PreferredBackBufferHeight = 600;
            //this.graphics.PreferredBackBufferWidth = 800;
            //this.graphics.ApplyChanges();

            recSprite[0] = new Rectangle(0, 0, 21, 36);
            recSprite[1] = new Rectangle(21, 0, 21, 36);
            recSprite[2] = new Rectangle(42, 0, 22, 36);
            recSprite[3] = new Rectangle(64, 0, 22, 36);
            recSprite[4] = new Rectangle(86, 0, 23, 36);
            recSprite[5] = new Rectangle(109, 0, 21, 36);
            recSprite[6] = new Rectangle(130, 0, 21, 36);
            recSprite[7] = new Rectangle(151, 0, 23, 36);
            recSprite[8] = new Rectangle(174, 0, 25, 36);
            recSprite[9] = new Rectangle(199, 0, 25, 36);
            recSprite[10] = new Rectangle(224, 0, 25, 36);
            recSprite[11] = new Rectangle(249, 0, 25, 36);
            recSprite[12] = new Rectangle(174, 0, 25, 36);
            recSprite[13] = new Rectangle(199, 0, 25, 36);
            recSprite[14] = new Rectangle(224, 0, 25, 36);
            recSprite[15] = new Rectangle(249, 0, 25, 36);
        }

        public override void LoadContent()
        {
            t2dTileSet = content.Load<Texture2D>(@"Map\Textures\fulltileset_alpha2");
            s_mainChar = new Sprite(content.Load<Texture2D>(@"Map\Sprites\mainChar"), new Vector2(408, 504));

            s_danceHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\danceHouse"), new Vector2(148 + Map_old.iMapDisplayOffsetX, 322 + Map_old.iMapDisplayOffsetY));
            s_forestHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\forestHouse"), new Vector2(6 + Map_old.iMapDisplayOffsetX, 4 + Map_old.iMapDisplayOffsetY));
            s_friendHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\friendHouse"), new Vector2(-2 + Map_old.iMapDisplayOffsetX, 408 + Map_old.iMapDisplayOffsetY));
            s_gamerHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\gamerHouse"), new Vector2(248 + Map_old.iMapDisplayOffsetX, 373 + Map_old.iMapDisplayOffsetY));
            s_mainCharHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\mainCharHouse"), new Vector2(407 + Map_old.iMapDisplayOffsetX, 379 + Map_old.iMapDisplayOffsetY));
            s_mansionHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\mansionHouse"), new Vector2(594 + Map_old.iMapDisplayOffsetX, 189 + Map_old.iMapDisplayOffsetY));
            s_restaurantHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\restaurantHouse"), new Vector2(-2 + Map_old.iMapDisplayOffsetX, 270 + Map_old.iMapDisplayOffsetY));
            s_riverHouse = new Sprite(content.Load<Texture2D>(@"Map\Sprites\riverHouse"), new Vector2(281 + Map_old.iMapDisplayOffsetX, 3 + Map_old.iMapDisplayOffsetY));

            s_rufus = new Sprite(content.Load<Texture2D>(@"Map\Sprites\rufus"), new Vector2(48 + Map_old.iMapDisplayOffsetX, 80 + Map_old.iMapDisplayOffsetY));
            s_bell = new Sprite(content.Load<Texture2D>(@"Map\Sprites\bell"), new Vector2(143 + Map_old.iMapDisplayOffsetX, 407 + Map_old.iMapDisplayOffsetY));
            s_goodwin = new Sprite(content.Load<Texture2D>(@"Map\Sprites\goodwin"), new Vector2(648 + Map_old.iMapDisplayOffsetX, 310 + Map_old.iMapDisplayOffsetY));
            s_line = new Sprite(content.Load<Texture2D>(@"Map\Sprites\line"), new Vector2(248 + Map_old.iMapDisplayOffsetX, 444 + Map_old.iMapDisplayOffsetY));
            s_clotilde = new Sprite(content.Load<Texture2D>(@"Map\Sprites\clotilde"), new Vector2(32 + Map_old.iMapDisplayOffsetX, 345 + Map_old.iMapDisplayOffsetY));

        }

        public override void Update(GameTime gameTime)
        {


            KeyboardState keyboardState = Keyboard.GetState();


            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            fTotalElapsedTime += elapsed;
            ksKeyboardState = Keyboard.GetState();


            // TODO: Add your update logic here
            fAnimationTime += elapsed;

            if (fAnimationTime >= fAnimationDelay)
            {
                iTileAnimationFrame++;
                if (iTileAnimationFrame > iTileAnimationFrameCount)
                {
                    iTileAnimationFrame = 0;
                }
                fAnimationTime = 0.0f;
            }


            //
            // Locomoção do personagem
            //
            if (keyboardState.GetPressedKeys().Length > 1) return;

            if (canWalk != iMapWalkable[(int)Math.Floor((s_mainChar.position.Y + 28 - iMapDisplayOffsetY) / iTileWidth), (int)Math.Floor((s_mainChar.position.X - iMapDisplayOffsetX) / iTileHeight)])
            {
                canWalk = (byte)iMapWalkable[(int)Math.Floor((s_mainChar.position.Y + 28 - iMapDisplayOffsetY) / iTileWidth), (int)Math.Floor((s_mainChar.position.X - iMapDisplayOffsetX) / iTileHeight)];
                decimal d = Convert.ToDecimal(s_mainChar.position.Y + 28f - iMapDisplayOffsetY);
                decimal d2 = Convert.ToDecimal(s_mainChar.position.X - iMapDisplayOffsetX);
                decimal d3 = (int)Math.Floor((s_mainChar.position.Y + 28 - iMapDisplayOffsetY) / iTileWidth);
                decimal d4 = (int)Math.Floor((s_mainChar.position.X - iMapDisplayOffsetX) / iTileHeight);
                Console.WriteLine(canWalk + " -> " + d.ToString("###") + "," + d2.ToString("###") + " -> " + d3.ToString("###") + "," + d4.ToString("###"));
            }



            if (keyboardState.IsKeyDown(Keys.Up))
            {
                if (CanWalk((int)s_mainChar.position.X, (int)s_mainChar.position.Y - 1))
                {
                    s_mainChar.position.Y -= 1;
                    if (Convert.ToInt32(s_mainChar.position.Y) - lastY < -5 || iMoveDirection != 0)
                    {
                        iMoveDirection = 0;
                        lastY = Convert.ToInt32(s_mainChar.position.Y);
                        m_Move = ((m_Move + 1) % 4) + 4;
                    }
                }


            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                if (CanWalk((int)s_mainChar.position.X, (int)s_mainChar.position.Y + 1))
                {
                    s_mainChar.position.Y += 1;
                    if (Convert.ToInt32(s_mainChar.position.Y) - lastY > 5 || iMoveDirection != 1)
                    {
                        iMoveDirection = 1;
                        lastY = Convert.ToInt32(s_mainChar.position.Y);
                        m_Move = (m_Move + 1) % 4;
                    }
                }
            }
            if (keyboardState.IsKeyDown(Keys.Left))
            {
                if (CanWalk((int)s_mainChar.position.X - 1, (int)s_mainChar.position.Y))
                {
                    s_mainChar.position.X -= 1;
                    if (Convert.ToInt32(s_mainChar.position.X) - lastX < -5 || iMoveDirection != 2)
                    {
                        iMoveDirection = 2;
                        lastX = Convert.ToInt32(s_mainChar.position.X);
                        m_Move = ((m_Move + 1) % 4) + 12;
                    }
                }
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                if (CanWalk((int)s_mainChar.position.X + 1, (int)s_mainChar.position.Y))
                {
                    s_mainChar.position.X += 1;
                    if (Convert.ToInt32(s_mainChar.position.X) - lastX > 5 || iMoveDirection != 3)
                    {
                        iMoveDirection = 3;
                        lastX = Convert.ToInt32(s_mainChar.position.X);
                        m_Move = ((m_Move + 1) % 4) + 8;
                    }
                }
            }

            #region WiiMote

            if (Input.WiimoteConnected)
            {
                Crosshair cursor1 = Input.GetCursor(Input.Cursors.Cursor1);
                if (cursor1.Visible)
                {
                    if (cursor1.Position.X < s_mainChar.position.X)
                    {
                        //move left 
                        if (CanWalk((int)s_mainChar.position.X - 1, (int)s_mainChar.position.Y))
                        {
                            s_mainChar.position.X -= 1;
                            if (Convert.ToInt32(s_mainChar.position.X) - lastX < -5 || iMoveDirection != 2)
                            {
                                iMoveDirection = 2;
                                lastX = Convert.ToInt32(s_mainChar.position.X);
                                m_Move = ((m_Move + 1) % 4) + 12;
                            }
                        }
                    }
                    else if (cursor1.Position.X > s_mainChar.position.X)
                    {
                        //move right
                        if (CanWalk((int)s_mainChar.position.X + 1, (int)s_mainChar.position.Y))
                        {
                            s_mainChar.position.X += 1;
                            if (Convert.ToInt32(s_mainChar.position.X) - lastX > 5 || iMoveDirection != 3)
                            {
                                iMoveDirection = 3;
                                lastX = Convert.ToInt32(s_mainChar.position.X);
                                m_Move = ((m_Move + 1) % 4) + 8;
                            }
                        }
                    }
                    if (cursor1.Position.Y < s_mainChar.position.Y)
                    {
                        //move up
                        if (CanWalk((int)s_mainChar.position.X, (int)s_mainChar.position.Y - 1))
                        {
                            s_mainChar.position.Y -= 1;
                            if (Convert.ToInt32(s_mainChar.position.Y) - lastY < -5 || iMoveDirection != 0)
                            {
                                iMoveDirection = 0;
                                lastY = Convert.ToInt32(s_mainChar.position.Y);
                                m_Move = ((m_Move + 1) % 4) + 4;
                            }
                        }
                    }
                    else if (cursor1.Position.Y > s_mainChar.position.Y)
                    {
                        //move down
                        if (CanWalk((int)s_mainChar.position.X, (int)s_mainChar.position.Y + 1))
                        {
                            s_mainChar.position.Y += 1;
                            if (Convert.ToInt32(s_mainChar.position.Y) - lastY > 5 || iMoveDirection != 1)
                            {
                                iMoveDirection = 1;
                                lastY = Convert.ToInt32(s_mainChar.position.Y);
                                m_Move = (m_Move + 1) % 4;
                            }
                        }
                    }
                }
            }

            #endregion

            //Test code
            if (Input.KeyPressed(Keys.C))
            {
                StartMiniGame(MiniGameType.Club);
            }

            if (Input.KeyPressed(Keys.R))
            {
                StartMiniGame(MiniGameType.Restaurant);
            }

            if (Input.KeyPressed(Keys.M))
            {
                StartMiniGame(MiniGameType.Microgames);
            }

            if (Input.KeyPressed(Keys.Escape))
            {
                PauseGame();
            }
        }

        private static void PauseGame()
        {
            GlobalContainer.gameStateManager.SetCurrentState(GameState.Paused);
        }

        public void StartMiniGame(MiniGameType gameType)
        {
            GlobalContainer.gameStateManager.SetActiveGame(gameType);     
        }

        private bool CanWalk(int x, int y)
        {
            //Melhorar isso
            return iMapWalkable[(int)Math.Floor((y + 28f - iMapDisplayOffsetY) / iTileWidth), (int)Math.Floor((x + 10f - iMapDisplayOffsetX) / iTileHeight)] == 1;
        }

        public override void Draw(GameTime gameTime)
        {
            //graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            //// TODO: Add your drawing code here

            int iTileToDraw;
            //graphics.GraphicsDevice.Clear(Color.Black);
            //graphics.GraphicsDevice.BeginScene;
            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteBlendMode.None);
            // Draw the map
            for (int y = 0; y < iMapDisplayHeight; y++)
            {
                for (int x = 0; x < iMapDisplayWidth; x++)
                {
                    iTileToDraw = iMap[y + iMapY, x + iMapX];
                    if (iTileToDraw == iTileAnimationStartFrame)
                    {
                        iTileToDraw += iTileAnimationFrame;
                    }
                    Rectangle recSource = new Rectangle((iTileToDraw % iTileSetXCount) * iTileWidth,
                                                        (iTileToDraw / iTileSetXCount) * iTileHeight,
                                                        iTileWidth, iTileHeight);
                    spriteBatch.Draw(t2dTileSet,
                                     new Rectangle(((x * iTileWidth) + iMapDisplayOffsetX) - iMapXOffset,
                                     ((y * iTileHeight) + iMapDisplayOffsetY) - iMapYOffset,
                                     iTileWidth, iTileHeight),
                                     recSource,
                                     Color.White);
                }
            }
            spriteBatch.End();
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            // Draw transitions layer of the map
            for (int y = 0; y < iMapDisplayHeight; y++)
            {
                for (int x = 0; x < iMapDisplayWidth; x++)
                {
                    Rectangle recDest = new Rectangle(((x * iTileWidth) + iMapDisplayOffsetX) - iMapXOffset,
                                         ((y * iTileHeight) + iMapDisplayOffsetY) - iMapYOffset,
                                         iTileWidth, iTileHeight);
                    iTileToDraw = iMapTrans[y + iMapY, x + iMapX];
                    if (iTileToDraw > 11)
                    {
                        Rectangle recSource = new Rectangle((iTileToDraw % iTileSetXCount) * iTileWidth,
                                                            (iTileToDraw / iTileSetXCount) * iTileHeight,
                                                            iTileWidth, iTileHeight);
                        spriteBatch.Draw(t2dTileSet, recDest, recSource, Color.White);
                    }

                    iTileToDraw = iMapObjects[y + iMapY, x + iMapX];
                    if (iTileToDraw > 11)
                    {
                        Rectangle recSource = new Rectangle((iTileToDraw % iTileSetXCount) * iTileWidth,
                                                            (iTileToDraw / iTileSetXCount) * iTileHeight,
                                                            iTileWidth, iTileHeight);
                        spriteBatch.Draw(t2dTileSet, recDest, recSource, Color.White);
                    }
                }
            }
            //spriteBatch.Draw(t2dGameScreen, new Rectangle(0, 0, 800, 600), Color.White);



            //Carrega as casas
            spriteBatch.Draw(s_mainCharHouse.texture, s_mainCharHouse.position, Color.White);
            spriteBatch.Draw(s_friendHouse.texture, s_friendHouse.position, Color.White);
            spriteBatch.Draw(s_mansionHouse.texture, s_mansionHouse.position, Color.White);
            spriteBatch.Draw(s_gamerHouse.texture, s_gamerHouse.position, Color.White);
            spriteBatch.Draw(s_forestHouse.texture, s_forestHouse.position, Color.White);

            spriteBatch.Draw(s_restaurantHouse.texture, s_restaurantHouse.position, Color.White);
            spriteBatch.Draw(s_riverHouse.texture, s_riverHouse.position, Color.White);
            spriteBatch.Draw(s_rufus.texture, s_rufus.position, Color.White);

            spriteBatch.Draw(s_goodwin.texture, s_goodwin.position, Color.White);
            spriteBatch.Draw(s_line.texture, s_line.position, Color.White);
            spriteBatch.Draw(s_clotilde.texture, s_clotilde.position, Color.White);
            //Carrega o mainchar
            if (m_Move > 11)
            {
                spriteBatch.Draw(s_mainChar.texture, s_mainChar.position, recSprite[m_Move], Color.White, MathHelper.ToRadians(0), Vector2.Zero, 1f, SpriteEffects.FlipHorizontally, 0f);
            }
            else
            {
                spriteBatch.Draw(s_mainChar.texture, s_mainChar.position, recSprite[m_Move], Color.White);
            }

            //Carrega as casas
            spriteBatch.Draw(s_danceHouse.texture, s_danceHouse.position, Color.White);
            spriteBatch.Draw(s_bell.texture, s_bell.position, Color.White);

            spriteBatch.End();
            
        }
    }

}
